package com.example.libary.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.libary.dto.BorrowRecordDTO;
import com.example.libary.entity.BorrowRecord;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

/**
 * 借阅记录Mapper接口
 */
@Mapper
public interface BorrowRecordMapper extends BaseMapper<BorrowRecord> {

    /**
     * 分页查询借阅记录详情（包含用户姓名和图书名称）
     */
    @Select("<script>" +
            "SELECT " +
            "br.id, br.user_id, br.book_id, br.borrow_date, " +
            "br.due_date as return_date, br.return_date as actual_return_date, " +
            "CASE " +
            "  WHEN br.status = '0' THEN 'BORROWED' " +
            "  WHEN br.status = '1' THEN 'RETURNED' " +
            "  WHEN br.status = '2' THEN 'OVERDUE' " +
            "  ELSE 'UNKNOWN' " +
            "END as status, " +
            "br.renew_count, br.fine, br.remark, br.create_time, " +
            "u.real_name as user_name, b.name as book_name " +
            "FROM borrow_record br " +
            "LEFT JOIN sys_user u ON br.user_id = u.id " +
            "LEFT JOIN book b ON br.book_id = b.id " +
            "WHERE br.deleted = 0 " +
            "<if test='ew != null and ew.sqlSegment != null and ew.sqlSegment != \"\"'>" +
            "AND ${ew.sqlSegment} " +
            "</if>" +
            "ORDER BY br.create_time DESC" +
            "</script>")
    Page<BorrowRecordDTO> selectPageWithDetails(Page<BorrowRecordDTO> page, @Param("ew") com.baomidou.mybatisplus.core.conditions.Wrapper<BorrowRecord> wrapper);
} 